package com.chefu.admin.controller;


import com.chefu.admin.pojo.ProviderUser;
import com.chefu.admin.pojo.ProviderVo;
import com.chefu.admin.pojo.ServiceProvider;
import com.chefu.admin.service.ProviderAdminService;
import com.chefu.admin.service.ServiceProviderService;
import com.chefu.common.exception.CfException;
import com.chefu.common.exception.ExceptionEnum;
import com.chefu.common.pojo.PageResult;
import com.chefu.common.pojo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author goodgoodstudy
 * @since 2021-03-30
 */
@RestController
@RequestMapping("/provider")
public class ProviderAdminController {

    @Autowired
    ProviderAdminService providerService;

    /**
     * 新增厂商主体
     */
    @PostMapping
    public R add(@RequestBody @Valid ServiceProvider serviceProvider, BindingResult result){
        if (result.hasErrors()){
            throw new CfException(ExceptionEnum.INVALID_PARAM_ERROR.getStatus(), result.getFieldError().getDefaultMessage());
        }
        providerService.add(serviceProvider);
        return R.ok();
    }

    /**
     * 分页查询厂商列表
     */
    @GetMapping("/{pageNo}/{pageSize}")
    public R findByPage(@PathVariable("pageNo") Long pageNo,
                        @PathVariable("pageSize") Long pageSize,
                        @RequestParam(required = false) Integer check,
                        @RequestParam(required = false) String name, @RequestParam(required = false)String code,
                        @RequestParam(required = false) Long city, @RequestParam(required = false)String phone,
                        @RequestParam(required = false)Boolean tChain, @RequestParam(required = false) Integer status){
        PageResult<ServiceProvider> pageInfo = providerService.findByPage(pageNo, pageSize, check, name, code, city, phone, tChain, status);
        return R.ok().data("pageInfo", pageInfo);
    }


    /**
     * 修改厂商主体
     */
    @PutMapping
    public R update(ServiceProvider serviceProvider){
        providerService.update(serviceProvider);
        return R.ok();
    }

    /**
     * 锁定厂商
     */
    @PutMapping("/lock")
    public R updateStatus(@RequestParam Long spId, @RequestParam Integer status){
        providerService.updateStatus(spId, status);
        return R.ok();
    }

    /**
     * 修改厂商审核状态
     */
    @PutMapping("/check")
    public R updateCheck(@RequestParam Long id, @RequestParam Integer check){
        providerService.updateCheck(id, check);
        return R.ok();
    }

    /**
     * 创建厂商管理员
     */
    @PostMapping("/user")
    public R addUser(@RequestBody ProviderUser providerUser){
        providerService.addUser(providerUser);
        return R.ok();
    }

    /**
     * 查看主体用户列表
     */
    @GetMapping("/user/{pid}")
    public R findProviderUserByPage(@PathVariable("pid") Long pid,
                                    @RequestParam(required = false, defaultValue = "1") Long pageNo,
                                    @RequestParam(required = false, defaultValue = "5") Long pageSize,
                                    @RequestParam(required = false)String username,
                                    @RequestParam(required = false)String realName,
                                    @RequestParam(required = false)String phone){
        PageResult<ProviderUser> pageInfo = providerService.findProviderUserByPage(pid, pageNo, pageSize, username, realName, phone);
        return R.ok().data("pageInfo", pageInfo);
    }

    /**
     * 重置厂商用户密码
     */
    @PutMapping("/user/resetPwd/{id}")
    public R resetUserPassword(@PathVariable("id")Long id){
        providerService.resetUserPassword(id);
        return R.ok();
    }
}

